#include "stdafx.h"

#include <MyControls/App.h>
#include <MyControls/MaskEditor.h>
#include <MyControls/Resource.h>

#include <imglib/bmp.h>
#include <imglib/winbmp.h>

namespace My { 

BEGIN_MESSAGE_MAP(MaskEditor, CCmdTarget)
	ON_COMMAND(ID_TOOL_BRUSH, OnToolBrush)
	ON_UPDATE_COMMAND_UI(ID_TOOL_BRUSH, OnUpdateToolBrush)
	ON_COMMAND(ID_TOOL_FILL, OnToolFill)
	ON_UPDATE_COMMAND_UI(ID_TOOL_FILL, OnUpdateToolFill)
	ON_COMMAND(ID_MASKEDIT_IMPORT_MASK, OnImportMask)
	ON_UPDATE_COMMAND_UI(ID_MASKEDIT_IMPORT_MASK, OnUpdateImportMask)
	ON_COMMAND(ID_MASKEDIT_EXPORT_MASK, OnExportMask)
	ON_UPDATE_COMMAND_UI(ID_MASKEDIT_EXPORT_MASK, OnUpdateExportMask)
END_MESSAGE_MAP()

// MaskEditor

MaskEditor::MaskEditor(ZoomBench & bench) :
	m_Bench(bench), 
	m_pOwner(0), 
	m_Brush(* this),
	m_Fill(* this),
	m_Sync(false)
{
	m_pTool = &m_Brush;
//	m_AcceleratorTable = LoadAccelerators(AfxGetInstanceHandle(), 
//		MAKEINTRESOURCE(IDR_ACCEL_EDITMASK));
}
	
MaskEditor::~MaskEditor()
{
	assert(!IsOpen());
}
		
void MaskEditor::Open(MaskOwner & owner)
{
	if (IsOpen())
		Close();
	m_pOwner = &owner;
	m_Buffer = m_pOwner->GetMask();
	DispatchEvent(StateChange(* this));
	m_Bench.SetActiveTool(m_pTool);
}

void MaskEditor::Close()
{
	if (!IsOpen())
		return;
	m_pTool = m_Bench.GetActiveTool();
	m_Bench.SetActiveTool(0);
	m_pOwner = 0;
	m_Buffer.clear();
	DispatchEvent(StateChange(* this));
}

void MaskEditor::Synchronize(const String & action)
{	 
	if (!IsOpen())
		return;
	m_Sync = true;
	m_pOwner->SetMask(m_Buffer, action);
	m_Sync = false;
	DispatchEvent(MaskChange(* this));
}

void MaskEditor::UpdateBuffer()
{
	if (!IsOpen() || m_Sync)
		return;
	m_Buffer = m_pOwner->GetMask();
	DispatchEvent(MaskChange(* this)); /// Pasha, why MaskChange? Not BufferChange?
}

void MaskEditor::OnToolBrush()
{
	if (!IsOpen())
		return;
	m_Bench.SetActiveTool(Brush());
}

void MaskEditor::OnUpdateToolBrush(CCmdUI * pCmdUI)
{
	pCmdUI->SetCheck(m_Bench.GetActiveTool() == Brush());
	pCmdUI->Enable(IsOpen() ? 1 : 0);
}

void MaskEditor::OnToolFill()
{
	if (!IsOpen())
		return;
	m_Bench.SetActiveTool(Fill());
}

void MaskEditor::OnUpdateToolFill(CCmdUI * pCmdUI)
{
	pCmdUI->SetCheck(m_Bench.GetActiveTool() == Fill());
	pCmdUI->Enable(IsOpen() ? 1 : 0);
}

void MaskEditor::OnImportMask()
{
	if (!IsOpen())
		return;

	CFileDialog dlg(TRUE, "bmp", NULL, OFN_HIDEREADONLY | OFN_FILEMUSTEXIST, 
		"Bitmap Files (*.bmp)|*.bmp||", GetApp().GetMainWnd());

	dlg.m_ofn.lpstrTitle = "Import Mask"; 
	if (dlg.DoModal() != IDOK) 
		return;

	GetApp().BeginActivity("importing mask");

	BitMask bmp;
	img::bmp::read(dlg.GetPathName(), bmp);
	if (bmp.dim() != m_pOwner->GetMask().dim()) {
		CString msg;
		msg.Format("The bitmap (%s) has different dimensions.\nProceed?", dlg.GetPathName());
		if (AfxMessageBox(msg, MB_ICONEXCLAMATION|MB_OKCANCEL, 0) == IDCANCEL)
			return;
		BitMask tmp;
		tmp.swap(bmp);
		bmp.resize(m_pOwner->GetMask().dim());
		bmp.fill(false);
		img::copy(bmp, tmp);
	}

	m_pOwner->SetMask(bmp, "Import Mask");
	m_Buffer = m_pOwner->GetMask();
	DispatchEvent(MaskChange(* this));

	GetApp().EndActivity();
}

void MaskEditor::OnUpdateImportMask(CCmdUI * pCmdUI)
{
	pCmdUI->Enable(IsOpen());
}

void MaskEditor::OnExportMask()
{
	if (!IsOpen())
		return;

	CFileDialog dlg(FALSE, "bmp", NULL, 
		OFN_HIDEREADONLY | OFN_PATHMUSTEXIST | OFN_OVERWRITEPROMPT, 
		"Bitmap Files (*.bmp)|*.bmp||", GetApp().GetMainWnd());

	dlg.m_ofn.lpstrTitle = "Export Mask"; 
	if (dlg.DoModal() != IDOK) 
		return;

	GetApp().BeginActivity("exporting mask");

	img::bmp::write1bit(dlg.GetPathName(), m_pOwner->GetMask());

	GetApp().EndActivity();
}

void MaskEditor::OnUpdateExportMask(CCmdUI * pCmdUI)
{
	pCmdUI->Enable(IsOpen());
}

} // My